package com.reden.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    // 判断文本与正则是否匹配
    public static boolean matches(String string, String regex) {
        if (ValidateUtil.isBlank(string)) throw new NullPointerException("Param 'string' can't be null or empty.");
        if (ValidateUtil.isBlank(regex)) throw new NullPointerException("Param 'regex' can't be null or empty.");
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        return matcher.matches();
    }

    // 判断文本是否包含对应正则
    public static boolean contain(String string, String regex) {
        if (ValidateUtil.isBlank(string)) throw new NullPointerException("Param 'string' can't be null or empty.");
        if (ValidateUtil.isBlank(regex)) throw new NullPointerException("Param 'regex' can't be null or empty.");
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        return matcher.find();
    }

    // 提取一份文本中符合正则的字符串
    public static String extract(String string, String regex) {
        if (ValidateUtil.isBlank(string)) throw new NullPointerException("Param 'string' can't be null or empty.");
        if (ValidateUtil.isBlank(regex)) throw new NullPointerException("Param 'regex' can't be null or empty.");
        String result = null;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        if (matcher.find()) {
            result = matcher.group();
        }
        return result;
    }

    // 提取全部文本中符合正则的字符串
    public static List<String> extracts(String string, String regex) {
        if (ValidateUtil.isBlank(string)) throw new NullPointerException("Param 'string' can't be null or empty.");
        if (ValidateUtil.isBlank(regex)) throw new NullPointerException("Param 'regex' can't be null or empty.");
        List<String> result = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            result.add(matcher.group());
        }
        return result;
    }

    // 获取一份文本中被指定正则包含的字符串
    public static String kernel(String string, String head, String tail) {
        if (ValidateUtil.isBlank(string)) throw new NullPointerException("Param 'string' can't be null or empty.");
        if (ValidateUtil.isBlank(head)) throw new NullPointerException("Param 'head' can't be null or empty.");
        if (ValidateUtil.isBlank(tail)) throw new NullPointerException("Param 'tail' can't be null or empty.");
        String result = null;
        String regex = head + ".*?" + tail;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        if (matcher.find()) {
            String temp = matcher.group();
            result = temp.substring(head.length(), temp.length() - tail.length());
        }
        return result;
    }

    // 获取全部文本中被指定正则包含的字符串
    public static List<String> kernels(String string, String head, String tail) {
        if (ValidateUtil.isBlank(string)) throw new NullPointerException("Param 'string' can't be null or empty.");
        if (ValidateUtil.isBlank(head)) throw new NullPointerException("Param 'head' can't be null or empty.");
        if (ValidateUtil.isBlank(tail)) throw new NullPointerException("Param 'tail' can't be null or empty.");
        List<String> result = new ArrayList<>();
        String regex = head + ".*?" + tail;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            String temp = matcher.group();
            result.add(temp.substring(head.length(), temp.length() - tail.length()));
        }
        return result;
    }
}
